﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Telerik.Web.UI;
using System.Globalization;

public partial class Report : BasePage
{
	protected override void pageLoad()
	{

		if (!IsPostBack)
		{
			tbDate.Text = DateTime.Now.AddDays(-14).ToString("MM/dd/yyyy");
			tbTo.Text = DateTime.Now.ToString("MM/dd/yyyy");

			if (Request.Cookies["ReportPageSize"] != null)
			{
				DropDownList1.SelectedValue = Request.Cookies["ReportPageSize"].Value;
			}

			ChangePageSize(int.Parse(DropDownList1.SelectedValue));


			if (!PAConfiguration.IsSuperUser(Teacher.TEACHER_ID))
			{
				this.ddlReportType.Items.RemoveAt(2);
			}
			bindSections();
		}
		else
		{
			if (Request.Cookies["ReportPageSize"] == null)
			{
				Response.Cookies.Set(new HttpCookie("ReportPageSize", DropDownList1.SelectedValue));
			}
			else
			{
				Response.Cookies.Set(Request.Cookies["ReportPageSize"]);
			}


			Response.Cookies["ReportPageSize"].Value = DropDownList1.SelectedValue;

		}

		ApplyChanges();

		if (Request.Params["__EVENTTARGET"] == "ctl00_ContentPlaceHolder1_gvReport")
		{
			string _params = Request.Params["__EVENTARGUMENT"];

			if (!string.IsNullOrEmpty(_params))
			{

				GridViewSortEventArgs e = new GridViewSortEventArgs(_params.Replace("Sort$", string.Empty), SortDirection.Ascending);
				gvReport_Sorting(this, e);
			}
		}

	}

	public string getHeader(DataTable dt)
	{
		StringBuilder sb = new StringBuilder();

		sb.Append("<table  name='headertable' class='stats' cellspacing='0' rules='all'  border='1' id='headertable' style='width:100%;border-collapse:collapse;' >");
		sb.Append("<tr class='hed2'>");
		string sortimage = string.Empty;

		foreach (DataColumn dc in dt.Columns)
		{
			if (dc.ColumnName == SortExpression)
			{
				if (isAsc)
				{
					sortimage = "<img src='images/darrow.gif' style='border-width:0px;' />";
				}
				else
				{
					sortimage = "<img src='images/uarrow.gif' style='border-width:0px;' />";
				}
			}
			else
			{
				sortimage = string.Empty;
			}

			sb.AppendFormat(
"<th scope='col'><a href=\"javascript:__doPostBack('ctl00_ContentPlaceHolder1_gvReport','Sort${1}')\">{0}</a>{2}</th>", dc.ColumnName, dc.ColumnName, sortimage);
		}

		sb.Append("</tr>");
		sb.Append("</table>");

		return sb.ToString();
	}

	protected void bindSections()
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;
		DataTable data = dal.GetSectionsShort(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, CurrentInfo.SemesterId, out message).Tables[0];
		CheckSections(data);
		this.Session["Sections"] = data;
		PALogManager.MessageLog(message);

		DataTable dt = data;
		DataRow dr = dt.NewRow();
		dr["SECTION_ID"] = 0;
		dr["SECTION_TITLE"] = " All Sections";
		dt.Rows.InsertAt(dr, 0);


		data.DefaultView.Sort = "SECTION_TITLE";
		this.ddlSections.DataSource = data;
		this.ddlSections.DataBind();
	}

	#region Properties
	public String Codes
	{
		get
		{
			string result = string.Empty;

			if (cbE.Checked)
			{
				result += cbE.Text;
			}

			if (cbF.Checked)
			{
				result += cbF.Text;
			}

			if (cbM.Checked)
			{
				result += cbM.Text;
			}

			if (cbO.Checked)
			{
				result += cbO.Text;
			}

			if (cbP.Checked)
			{
				result += cbP.Text;
			}

			if (cbQ.Checked)
			{
				result += cbQ.Text;
			}

			if (cbW.Checked)
			{
				result += cbW.Text;
			}

			return result;
		}
	}
	public DateTime From
	{
		get
		{

			DateTimeFormatInfo dp = new DateTimeFormatInfo();
			dp.FullDateTimePattern = "MM/dd/yyyy";
			return DateTime.Parse(tbDate.Text, dp);
		}
	}

	public DateTime To
	{
		get
		{
			DateTimeFormatInfo dp = new DateTimeFormatInfo();
			dp.FullDateTimePattern = "MM/dd/yyyy";
			return DateTime.Parse(tbTo.Text, dp);
		}
	}
	#endregion

	public override string Icon
	{
		get
		{
			return "images/report_icon.png";
		}
	}
	protected void bGo_Click(object sender, ImageClickEventArgs e)
	{
		rgReport.Visible = true;
		lbExport.Visible = true;
		Fill();

	}

	protected override void Fill()
	{
		if (ddlSections.SelectedValue == string.Empty)
			return;

		int reportType = int.Parse(ddlReportType.SelectedValue);
		DataSet results = null;
		DataLayer dal = new DataLayer(); string message = string.Empty;
		if (reportType == 1)
		{


			results = dal.GetDemographicReport(PAConfiguration.ConnectionString,
				decimal.Parse(ddlSections.SelectedValue),
				Teacher.TEACHER_ID,
				CurrentInfo.SemesterId,
				out message);


		}
		if (reportType == 2)
		{
			results = dal.AllStudentsAttendanceReport(PAConfiguration.ConnectionString,
				decimal.Parse(ddlSections.SelectedValue),
				Teacher.TEACHER_ID,
				CurrentInfo.SemesterId,
				out message);
		}

		if (reportType == 3)
		{
			results = dal.TeachersActivityReport(PAConfiguration.ConnectionString,
				decimal.Parse(ddlSections.SelectedValue),
				Teacher.TEACHER_ID,
				CurrentInfo.SemesterId,
				out message);
		}

		if (reportType == 4)
		{
			results = dal.CourseAttendanceReport(PAConfiguration.ConnectionString,
				CurrentInfo.SemesterId,
				out message);
		}

		if (reportType == 5)
		{
			results = dal.DeatiledAttendanceReport(PAConfiguration.ConnectionString,
				From,
				To,
				Codes,
				out message);
		}

		PALogManager.MessageLog(message);


		if ((results == null) || (results.Tables.Count == 0))
		{
			rgReport.DataSource = null;
			rgReport.DataBind();
			return;
		}
		DataTable dt = results.Tables[0];

		if (!string.IsNullOrEmpty(Sorting))
		{
			dt.DefaultView.Sort = Sorting;
		}



		rgReport.DataSource = dt;
		rgReport.DataBind();

	}

	protected void lbExport_Click(object sender, ImageClickEventArgs e)
	{
		if (decimal.Parse(ddlSections.SelectedValue) < 0)
			return;
		string reportName = "";

		int reportType = int.Parse(ddlReportType.SelectedValue);
		DataTable toExcel = null;
		DataLayer dal = new DataLayer(); string message = string.Empty;
		if (reportType == 1)
		{


			toExcel = dal.GetDemographicReport(PAConfiguration.ConnectionString,
				decimal.Parse(ddlSections.SelectedValue),
				Teacher.TEACHER_ID,
				CurrentInfo.SemesterId,
				out message).Tables[0];
			reportName = "Demographics_Report.csv";

		}
		if (reportType == 2)
		{
			toExcel = dal.AllStudentsAttendanceReport(PAConfiguration.ConnectionString,
				decimal.Parse(ddlSections.SelectedValue),
				Teacher.TEACHER_ID,
				CurrentInfo.SemesterId,
				out message).Tables[0];
			reportName = "All_Attend_Report.csv";

		}

		if (reportType == 3)
		{
			toExcel = dal.TeachersActivityReport(PAConfiguration.ConnectionString,
				decimal.Parse(ddlSections.SelectedValue),
				Teacher.TEACHER_ID,
				CurrentInfo.SemesterId,
				out message).Tables[0];
			reportName = "Teachers_Activity_Report.csv";

		}

		if (reportType == 4)
		{
			toExcel = dal.CourseAttendanceReport(PAConfiguration.ConnectionString,
				CurrentInfo.SemesterId,
				out message).Tables[0];
			reportName = "Attendance_By_Course.csv";

		}

		if (reportType == 5)
		{
			toExcel = dal.DeatiledAttendanceReport(PAConfiguration.ConnectionString,
				From,
				To,
				Codes,
				out message).Tables[0];
			reportName = "Attendance_Detailed.csv";

		}

		PALogManager.MessageLog(message);
		Manager.ExportToCSV(
			toExcel,
			this.Context,
			reportName);
	}
	protected void gvReport_Sorting(object sender, GridViewSortEventArgs e)
	{
		Sort(e);
	}



	protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
	{
		Sorting = string.Empty;

		this.rgReport.MasterTableView.SortExpressions.Clear();
		this.rgReport.Visible = false;
		lbExport.Visible = false;
		this.rgReport.CurrentPageIndex = 0;

		ApplyChanges();
	}

	protected void ApplyChanges()
	{
		if (ddlReportType.SelectedValue != string.Empty)
			DemographicsReportFilter.Visible = (decimal.Parse(ddlReportType.SelectedValue) == 1);

		if (ddlReportType.SelectedValue != string.Empty)
			DetailedAttendanceReport.Visible = (decimal.Parse(ddlReportType.SelectedValue) == 5);
	}

	protected void ChangePageSize(int pagesize)
	{

		if (pagesize > 0)
		{
			rgReport.PageSize = pagesize;
			rgReport.AllowPaging = true;
		}
		else
			rgReport.AllowPaging = false;

	}


	protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
	{
		ChangePageSize(int.Parse(DropDownList1.SelectedValue));

		Fill();
	}

	protected void rgReport_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
	{
		Sort(e);
	}

	protected void rgReport_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
	{
		Fill();
	}
	protected void rgReport_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
	{
		if ((e.Item.ItemType != GridItemType.AlternatingItem) &&
		(e.Item.ItemType != GridItemType.Item))
			return;

		if (decimal.Parse(ddlReportType.SelectedValue) == 1)
		{
			e.Item.Cells[4].HorizontalAlign = HorizontalAlign.Left;

			e.Item.Cells[11].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[12].HorizontalAlign = HorizontalAlign.Center;

		}

		if (decimal.Parse(ddlReportType.SelectedValue) == 2)
		{
			e.Item.Cells[2].HorizontalAlign = HorizontalAlign.Left;
			e.Item.Cells[4].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[8].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[9].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[10].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[11].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[12].HorizontalAlign = HorizontalAlign.Center;

		}

		if (decimal.Parse(ddlReportType.SelectedValue) == 3)
		{
			e.Item.Cells[6].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[7].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[8].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[9].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[10].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[11].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[12].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[13].HorizontalAlign = HorizontalAlign.Center;

		}

		if (decimal.Parse(ddlReportType.SelectedValue) == 4)
		{
			e.Item.Cells[6].HorizontalAlign = HorizontalAlign.Center;

			e.Item.Cells[9].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[10].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[11].HorizontalAlign = HorizontalAlign.Center;
			e.Item.Cells[12].HorizontalAlign = HorizontalAlign.Center;
		}

		if (decimal.Parse(ddlReportType.SelectedValue) == 5)
		{
			
			e.Item.Cells[3].HorizontalAlign = HorizontalAlign.Center;
			
			e.Item.Cells[5].HorizontalAlign = HorizontalAlign.Center;

			for (int i = 6; i < e.Item.Cells.Count-1; i++)
			{
				e.Item.Cells[i].HorizontalAlign = HorizontalAlign.Center;
			}
		}
	}
}
